package com.ibeeking.found.msg.rest.controller.v1;

import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.common.enums.PublishStatusEnum;
import com.ibeeking.found.msg.rest.service.IEmailConfigService;
import com.ibeeking.found.msg.rest.service.IEmailTemplateService;

import com.ibeeking.found.msg.service.common.param.EmailConfigParam;
import com.ibeeking.found.msg.service.common.param.EmailTemplateParam;
import com.ibeeking.found.msg.service.common.query.EmailTemplateQuery;
import com.ibeeking.found.msg.service.common.vo.EmailTemplateParamVO;
import com.ibeeking.found.msg.service.common.vo.EmailTemplateVO;
import com.ibeeking.found.msg.service.common.vo.EmailConfigVO;
import com.ibeeking.nematos.constants.enums.LogClassifyEnum;
import com.ibeeking.nematos.constants.enums.LogSourceEnum;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.base.BaseValidationGroup;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName EmailConfigController控制器
 * @Description
 * @Author ibeeking
 * @Date 2020-12-09 10:54
 **/
@RestController
@RequestMapping("/msg/emailConfig/v1")
public class EmailConfigController {

    @Resource
    private IEmailConfigService emailConfigServiceImpl;

    @Resource
    private IEmailTemplateService emailConfigTemplateServiceImpl;

    /*
    *  邮件服务器配置
     */
    @Log(logSource = LogSourceEnum.TENANT_LOG,logClassify = LogClassifyEnum.OPERATION_LOG,logType = OperateLogTypeEnum.QUERY, describe = "邮件配置分页查询")
    @PostMapping(value = "/listEmailConfig")
    public List<EmailConfigVO> listEmailConfig() {
        return emailConfigServiceImpl.listEmailConfig();
    }

    @Log(logSource = LogSourceEnum.TENANT_LOG,logClassify = LogClassifyEnum.OPERATION_LOG,logType = OperateLogTypeEnum.MODIFY, describe = "邮件配置修改")
    @PostMapping(value = "/modify")
    public Boolean modify(@RequestBody @Validated(BaseValidationGroup.Update.class) EmailConfigParam emailConfigParam) {
        return emailConfigServiceImpl.modifyEmailConfig(emailConfigParam);
    }

    @Log(logSource = LogSourceEnum.TENANT_LOG,logClassify = LogClassifyEnum.OPERATION_LOG,logType = OperateLogTypeEnum.QUERY, describe = "邮件配置查看")
    @GetMapping(value = "/view/{id}")
    public EmailConfigVO view(@PathVariable("id") Long id) {
        return emailConfigServiceImpl.viewEmailConfig(id);
    }

    @Log(logSource = LogSourceEnum.TENANT_LOG,logClassify = LogClassifyEnum.OPERATION_LOG,logType = OperateLogTypeEnum.MODIFY, describe = "邮件配置启用/禁用")
    @PostMapping(value = "/changePublishStatus/{id}/{status}")
    public Boolean changePublishStatus(@LoginUser CurrentUser currentUser, @PathVariable("id") Long id, @PathVariable("status") Integer status, HttpServletRequest request) {
        return emailConfigServiceImpl.changePublishStatus(id, status);
    }

    @Log(logSource = LogSourceEnum.TENANT_LOG,logClassify = LogClassifyEnum.OPERATION_LOG,logType = OperateLogTypeEnum.MODIFY, describe = "邮件缺省配置")
    @PostMapping(value = "/changeDefaultStatus/{id}/{status}")
    public Boolean changeDefaultStatus(@LoginUser CurrentUser currentUser, @PathVariable("id") Long id, @PathVariable("status") Integer status, HttpServletRequest request) {
        return emailConfigServiceImpl.changeDefaultStatus(id, status);
    }

    /*
     *  邮件模板配置
     */
    @Log(logSource = LogSourceEnum.TENANT_LOG,logClassify = LogClassifyEnum.OPERATION_LOG,logType = OperateLogTypeEnum.QUERY, describe = "邮件模板列表查询")
    @PostMapping(value = "/listEmailTemplate")
    public List<EmailTemplateVO> listEmailTemplate() {
        return emailConfigTemplateServiceImpl.listEmailTemplate();
    }

    @Log(logSource = LogSourceEnum.TENANT_LOG,logClassify = LogClassifyEnum.OPERATION_LOG,logType = OperateLogTypeEnum.QUERY, describe = "邮件模板列表查询")
    @PostMapping(value = "/listEffectEmailTemplate")
    public List<EmailTemplateVO> listEffectEmailTemplate(@RequestBody EmailTemplateQuery query) {
        query.setPublishStatus(PublishStatusEnum.ENABLE.getValue());
        return emailConfigTemplateServiceImpl.list(query);
    }
    /*
     *  邮件模板参数配置
     */
    @Log(logSource = LogSourceEnum.TENANT_LOG,logClassify = LogClassifyEnum.OPERATION_LOG,logType = OperateLogTypeEnum.QUERY, describe = "邮件模板参数列表查询")
    @PostMapping(value = "/listEmailTemplateParam/{templateId}")
    public List<EmailTemplateParamVO> listEmailTemplateParam(@PathVariable("templateId") Long templateId) {
        return emailConfigTemplateServiceImpl.listEmailTemplateParam(templateId);
    }

    @Log(logSource = LogSourceEnum.TENANT_LOG,logClassify = LogClassifyEnum.OPERATION_LOG,logType = OperateLogTypeEnum.QUERY, describe = "邮件模板查看")
    @GetMapping(value = "/viewEmailTemplate/{id}")
    public EmailTemplateVO viewEmailTemplate(@PathVariable("id") Long id) {
        return emailConfigTemplateServiceImpl.viewEmailTemplate(id);
    }

    @Log(logSource = LogSourceEnum.TENANT_LOG,logClassify = LogClassifyEnum.OPERATION_LOG,logType = OperateLogTypeEnum.QUERY, describe = "邮件模板查看")
    @GetMapping(value = "/viewEmailTemplateByCode/{code}")
    public EmailTemplateVO viewEmailTemplateByCode(@PathVariable("code") String code) {
        return emailConfigTemplateServiceImpl.queryTemplateByCode(code);
    }

    @Log(logSource = LogSourceEnum.TENANT_LOG,logClassify = LogClassifyEnum.OPERATION_LOG,logType = OperateLogTypeEnum.MODIFY, describe = "邮件模板修改")
    @PostMapping(value = "/modifyEmailTemplate")
    public Boolean modifyEmailTemplate( @RequestBody @Validated(BaseValidationGroup.Update.class) EmailTemplateParam emailTemplateParam) {
        return emailConfigTemplateServiceImpl.modifyEmailTemplate(emailTemplateParam);
    }

    @Log(logSource = LogSourceEnum.TENANT_LOG,logClassify = LogClassifyEnum.OPERATION_LOG,logType = OperateLogTypeEnum.MODIFY, describe = "邮件模板启用/禁用")
    @PostMapping(value = "/changeTemplatePublishStatus/{id}/{status}")
    public Boolean changeTemplatePublishStatus( @PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return emailConfigTemplateServiceImpl.changePublishStatus(id, status);
    }

}
